import java.util.Scanner;

/**
 * Description:
 * 通讯录显示类
 * 1.显示主菜单和与用户控制台交互信息
 * 2.对用户输入的信息作简单校验。
 *
 * User:Jiali
 * Date:2022-08-06-16:06
 */
public class ContactsView {
    ContactsController ctrl = new ContactsController();
    Scanner scanner = new Scanner(System.in);

    /**
     * 菜单显示
     */
    public void menu(){
        System.out.println("+----------- ContactMenu  ------------+");
        System.out.println("|                                     |");
        System.out.println("|             1.Insert                |");
        System.out.println("|                                     |");
        System.out.println("|             2.Remove                |");
        System.out.println("|                                     |");
        System.out.println("|             3.Modify                |");
        System.out.println("|                                     |");
        System.out.println("|             4.Search                |");
        System.out.println("|                                     |");
        System.out.println("|             5.ShowAll               |");
        System.out.println("|                                     |");
        System.out.println("|             6.SortByName            |");
        System.out.println("|                                     |");
        System.out.println("|             7.ClearAll              |");
        System.out.println("|                                     |");
        System.out.println("|             0.Exit                  |");
        System.out.println("|                                     |");
        System.out.println("+------------ Version 1.0 ------------+");
    }

    /**
     * 主菜单入口
     */
    public void start(){
        int menuSelection = 0;

        do{
            menu();
            System.out.println("请选择：");
            menuSelection = scanner.nextInt();
            scanner.nextLine();

            switch (menuSelection){
                case 1:
                    insert();
                    break;
                case 2:
                    remove();
                    break;
                case 3:
                    modify();
                    break;
                case 4:
                    search();
                    break;
                case 5:
                    showAll();
                    break;
                case 6:
                    sort();
                    break;
                case 7:
                    clearAll();
                    break;
                case 0:
                    saveData();
                    System.out.println("退出通讯录。");
                    break;
                default:
                    System.out.println("选择错误!");
                    break;
            }
        }while(menuSelection != 0);
    }

    /**
     * 新增联系人
     */
    private void insert() {
        System.out.println("新增联系人...\n");
        System.out.print("姓名：");
        String name = scanner.nextLine();
        System.out.print("性别：");
        String gender = scanner.next();
        System.out.print("年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();//过滤前一行末尾空格
        System.out.print("电话：");
        String phoneNumber = scanner.nextLine();
        System.out.print("备注：");
        String notes = scanner.nextLine();

        ctrl.insertElement(new Person(name,gender,age,phoneNumber,notes));
        System.out.println("\"" + name + "\"已添加到通讯录。");
    }

    /**
     * 删除联系人
     */
    private void remove() {
        if(ctrl.getCurrentCount() == 0){
            System.out.println("当前通讯录为空！");
            return;
        }
        System.out.println("请输入您要删除的联系人姓名：");
        String name = scanner.nextLine();
        if(!name.equals(" ") && name.length() != 0){ //检测姓名的合法性
            boolean isRemove = ctrl.removeElement(name);
            if(isRemove){
                System.out.println("已删除\"" + name + "\"!");
            }else{
                System.out.println("通讯录中未找到\"" + name +"\"!");
            }
        }else{
            System.out.println("姓名输入有误！");
        }
    }

    /**
     * 修改联系人
     */
    private void modify(){
        if(ctrl.getCurrentCount() == 0){
            System.out.println("当前通讯录为空！");
            return;
        }

        System.out.println("请输入您要修改的联系人姓名：");
        String name = scanner.nextLine();
        if(!name.equals(" ") && name.length() != 0){ //检测姓名的合法性
            Person person = ctrl.searchByname(name);
            if(person != null){
                System.out.println("联系人信息");
                System.out.println("姓名:" + person.getName());
                System.out.println("性别:" + person.getGender());
                System.out.println("年龄:" + person.getAge());
                System.out.println("电话:" + person.getPhoneNumber());
                System.out.println("备注:" + person.getNotes());

                System.out.println("\n修改各项信息:");
                System.out.print("姓名：");
                String changedName = scanner.nextLine();
                System.out.print("性别：");
                String gender = scanner.nextLine();
                System.out.print("年龄：");
                int age = scanner.nextInt();
                scanner.nextLine();//过滤前一行末尾空格
                System.out.print("电话：");
                String phoneNumber = scanner.nextLine();
                System.out.print("备注：");
                String notes = scanner.nextLine();

                ctrl.modifyElement(name,new Person(changedName,gender,age,phoneNumber,notes));
                System.out.println("修改成功!");
            }else{
                System.out.println("通讯录中未找到\"" + name +"\"!");
            }

        }else{
            System.out.println("姓名输入有误！");
        }
    }

    /**
     * 查找联系人
     */
    private void search(){
        if(ctrl.getCurrentCount()== 0){
            System.out.println("当前通讯录为空！");
            return;
        }

        System.out.println("查找联系人...");
        System.out.println("请输入您要查找的联系人姓名:");
        String name = scanner.nextLine();
        if(!name.equals(" ") && name.length() != 0) { //检测姓名的合法性
            Person person = ctrl.searchByname(name);
            if(person != null){
                    System.out.println("联系人信息" );
                    System.out.println("姓名:" + person.getName());
                    System.out.println("性别:" + person.getGender());
                    System.out.println("年龄:" + person.getAge());
                    System.out.println("电话:" + person.getPhoneNumber());
                    System.out.println("备注:" + person.getNotes());
                    System.out.println();
            }else{
                System.out.println("通讯录中未找到\"" + name +"\"!");
            }
        }else{
            System.out.println("姓名输入有误！");
        }
    }

    /**
     * 显示全部联系人信息
     */
    private void showAll() {
        if(ctrl.getCurrentCount() == 0){
            System.out.println("当前通讯录为空！");
            return;
        }
        System.out.print("+----------------------------- 全部联系人信息 -----------------------------+\n");
        System.out.println(" 姓名\t\t性别\t\t年龄\t\t电话\t\t\t\t备注\t");
        Person[] people = ctrl.getPeople();
        for (int i = 0; i < ctrl.getCurrentCount(); i++)
        {
            System.out.printf(" %-11s%-8s%-8d%-16s%-16s\n",people[i].getName(),
                    people[i].getGender(),people[i].getAge(),people[i].getPhoneNumber(),
                    people[i].getNotes());
        }
        System.out.printf("+-----------------------------  共%d位联系人  -----------------------------+\n",ctrl.getCurrentCount());

    }

    /**
     * 排序联系人信息
     */
    private void sort(){
        if(ctrl.getCurrentCount() == 0){
            System.out.println("当前通讯录为空！");
            return;
        }
        ctrl.sortByName();
        System.out.println("排序完毕！");
    }


    /**
     * 清空所有联系人信息
     */
    private void clearAll(){
        if(ctrl.getCurrentCount() == 0){
            System.out.println("当前通讯录已为空！");
            return;
        }
        System.out.println("您确定要清空通讯录吗？此操作不可逆哦！");
        System.out.println("确认：Yes|yes，取消：No|no");
        String isClear = scanner.nextLine();
        if("Yes".equalsIgnoreCase(isClear)){
            ctrl.clearAll();
            System.out.println("清空完毕！");
        }
    }

    /**
     * 保存联系人数据
     */
    private void saveData(){
        ctrl.saveData();
    }

}

